package cn.swh.guguanjia.mapper;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/10/28 12:01
 * @Version: 1.0
 */
public class WorkOrderProvider {
    public String selectPage(Map<String, Object> params) {
        return new SQL() {{
            SELECT("wo.*,cu.NAME create_user_name," +
                    "tu.NAME transfer_user_name," +
                    "ru.NAME recipient_user_name," +
                    "co.NAME create_office_name");
            FROM("work_order wo");
            LEFT_OUTER_JOIN("sys_user cu ON wo.create_user_id = cu.id " +
                    " LEFT JOIN sys_user tu ON wo.transport_user_id = tu.id " +
                    " LEFT JOIN sys_user ru ON wo.recipient_user_id = ru.id " +
                    " LEFT JOIN sys_office co ON cu.office_id = co.id " +
                    " LEFT JOIN sys_office `to` ON tu.office_id = `to`.id " +
                    " LEFT JOIN sys_office ro ON ru.office_id = ro.id");
            WHERE("wo.del_flag='0'");
            if (params.containsKey("status") && !StringUtils.isEmpty(params.get("status"))){
                WHERE("  wo.status=#{status}");
            }
            if(params.containsKey("startDate")&& !StringUtils.isEmpty(params.get("startDate"))){
                WHERE("wo.create_date>=#{startDate}");
            }
            if(params.containsKey("endDate")&& !StringUtils.isEmpty(params.get("endDate"))){
                WHERE("wo.create_date<=#{endDate}");
            }
            if(params.containsKey("oid")&&!StringUtils.isEmpty(params.get("oid"))){
                WHERE("  (co.id=#{oid}  or `to`.id=#{oid}   or ro.id=#{oid}  )");
            }
        }}.toString();
    }
}
